package com.vito.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

/***************************************************************
 * File name: DataValidUtil.java     Created on 2008-8-31
 * Description: [导入数据校验]
 * Company:     东软集团股份有限公司
 * Department:  软件开发事业部
 * @author      [赵温涛] zhaowentao@neusoft.com
 * ------------------------------------------------------------
 * 修改历史
 * 序号  日期  修改人   修改原因
 * 1
 * 2
 *-------------------------------------------------------------
 ***************************************************************/
public class DataValidateUtil {
    /**
     * Create date:2008-08-06
     * Method name:notNull
     * Description: 非空验证
     * return boolean
     */
    public static boolean notNull(String str) {
        return !(str == null || "".equals(str.trim()));
    }
    /**
     * Create date:2008-08-06
     * Method name:notNull
     * Description: 非空验证
     * return boolean
     */
    public static boolean isNull(String str) {
        return (str == null || "".equals(str.trim()));
    }
    /**
     * Create date:2008-08-06
     * Method name:getLength
     * Description: 获取字符串字节数
     * return int
     */
    public static int getLength(String str) {
        if(!notNull(str)){
            return 0;
        }
        String regex = "[^\\x00-\\xff]";//长度验证
        return str.replaceAll(regex, "**").length();
    }

    /**
     * Create date:2008-08-06
     * Method name:isNumOrChar
     * Description: 数字、字符、圆点、横线 验证
     * return boolean
     */
    public static boolean isNumOrChar(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex = "\\w+[\\w-.]*"; //数字或字符验证
        return Pattern.matches(regex, str);
    }

    /**
     * Create date:2008-08-29
     * Method name:formatAfterDotNumber
     * Description: 平均分数格式转换
     * return boolean
     */
    public static String formatAfterDotNumber( String Str, int nAfterDotNum ){
        BigDecimal b = new BigDecimal(Str);
        BigDecimal one = new BigDecimal("1");
        double result =  b.divide(one,nAfterDotNum,BigDecimal.ROUND_HALF_UP).doubleValue();
        return Double.toString(result);
       }

    /**
     * Create date:2008-08-29
     * Method name:testDate
     * Description: 200806格式校验
     * return boolean
     */
    public static boolean testDate(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex = "^([1-2]\\d{3})((0[1-9])|(1[0-2]))$"; //200506格式验证
        return Pattern.matches(regex, str);
    }

    /**
     * Create date:2008-08-06
     * Method name:isNatrual
     * Description: 自然数验证
     * return boolean
     */
    public static boolean isNatrual(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex = "0|([1-9]\\d*)"; //自然数验证
        return Pattern.matches(regex, str);
    }

    /**
     * Create date:2008-08-06
     * Method name:isNumber
     * Description: 数值验证
     * return boolean
     */
    public static boolean isNumber(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex = "\\d*"; //自然数验证
        return Pattern.matches(regex, str);
    }
    /**
     * Create date:2008-08-06
     * Method name:isFloat
     * Description: 小数验证
     * return boolean
     */
    public static boolean isFloat(String str,double minFloat,double maxFloat) {
        if(!notNull(str)){
            return true;
        }
        String regex = "^(([0-9]|([1-9]\\d*))(\\.\\d+)?)$";
        if(Pattern.matches(regex, str)){
        	if(Double.parseDouble(str) >= minFloat && Double.parseDouble(str) <= maxFloat){
                return true;
        	}
        }
        return false;
    }

    /**
     * Create date:2008-08-06
     * Method name:isIP
     * Description: IP地址验证
     * return boolean
     */
    public static boolean isIP(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; //自然数验证
        if(!Pattern.matches(regex, str)){
            return false;
        }
        String[] ip = str.split("\\.");
        for(int i=0;i<ip.length;i++){
	        if(!(isNatrual(ip[i])&&Integer.parseInt(ip[i])>=0&&Integer.parseInt(ip[i])<=255)){
	        	return false;
	        }
        }
        return true;
    }
    /**
     * Create date:2008-08-06
     * Method name:notSpecial
     * Description: 特殊字符校验不能包含`@#$%^&*|
     * return boolean
     */
    public static boolean notSpecial(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex = "[^\\`\\@\\#\\$\\%\\^\\&\\*\\|]*"; //特殊字符校验
        return Pattern.matches(regex, str);
    }

    /**
     * Create date:2008-08-06
     * Method name:isEmail
     * Description: Email验证
     * return boolean
     */
    public static boolean isEmail(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; //Email验证
        return Pattern.matches(regex, str);
    }

    /**
     * Create date:2009-02-20
     * Method name:isMobile
     * Description: 手机验证
     * return boolean
     */
    public static boolean isMobile(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex = "^((\\(\\d{2,3}\\))|(\\d{3}\\-))?1\\d{10}$"; //Mobile验证
        return Pattern.matches(regex, str);
    }

    /**
     * Create date:2009-02-20
     * Method name:isPhone
     * Description: 电话验证
     * return boolean
     */
    public static boolean isPhone(String str) {
        if(!notNull(str)){
            return true;
        }
        if(isMobile(str)){
            return true;
        }
        String regex = "^(((\\(\\d{2,3}\\))|(\\d{2,3}\\-))?(\\(0\\d{2,3}\\)|0\\d{2,3})?-?[1-9]\\d{6,7}(\\-\\d{1,4})?)$"; //Email验证
        return Pattern.matches(regex, str);
    }
    /**
     * Create date:2008-08-12
     * Method name:isASCII
     * Description: ASCII字符
     * return boolean
     */
    public static boolean isASCII(String str) {
        if(!notNull(str)){
            return true;
        }
        String regex="[\\x00-\\xff]*";//ASCII字符
        return Pattern.matches(regex, str);
    }

    /**
     * Create date:2008-08-06
     * Method name:isDate
     * Description: 日期验证
     * return String[] String[0]：日期合法标识 1合法，0不合法；String[1]：输出yyyy-MM-dd格式的日期
     */
    public static String[] isDate(String str) {
        if(!notNull(str)){
            return new String[] { "1", "" };
        }
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy_MM_dd");
        SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat sdf5 = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat sdf6 = new SimpleDateFormat("dd_MM_yyyy");
        SimpleDateFormat sdf7 = new SimpleDateFormat("dd/MM/yyyy");

        String regex1 = "\\d{8}";
        String regex2 = "\\d{4}\\-\\d{1,2}\\-\\d{1,2}";
        String regex3 = "\\d{4}\\_\\d{1,2}\\_\\d{1,2}";
        String regex4 = "\\d{4}\\/\\d{1,2}\\/\\d{1,2}";
        String regex5 = "\\d{1,2}\\-\\d{1,2}\\-\\d{4}";
        String regex6 = "\\d{1,2}\\_\\d{1,2}\\_\\d{4}";
        String regex7 = "\\d{1,2}\\/\\d{1,2}\\/\\d{4}";
        String[] objs = new String[] { "0", "" };
        if (Pattern.matches(regex1, str)) {
            try {
                objs[1] = sdf1.format(sdf1.parse(str.trim()));
                objs[0] = "1";
            }
            catch (Exception E) {
                objs[0] = "0";
                objs[1] = str+"不能转换为"+"yyyyMMdd格式的日期！";
            }
        }
        else if (Pattern.matches(regex2, str)) {
            try {
                objs[1] = sdf2.format(sdf2.parse(str.trim()));
                objs[0] = "1";
            }
            catch (Exception E) {
                objs[0] = "0";
                objs[1] = str+"不能转换为"+"yyyy-MM-dd格式的日期！";
            }
        }
        else if (Pattern.matches(regex3, str)) {
            try {
                objs[1] = sdf3.format(sdf3.parse(str.trim()));
                objs[0] = "1";
            }
            catch (Exception E) {
                objs[0] = "0";
                objs[1] = str+"不能转换为"+"yyyy_MM_dd格式的日期！";
            }
        }
        else if (Pattern.matches(regex4, str)) {
            try {
                objs[1] = sdf4.format(sdf4.parse(str.trim()));
                objs[0] = "1";
            }
            catch (Exception E) {
                objs[0] = "0";
                objs[1] = str+"不能转换为"+"yyyy/MM/dd格式的日期！";
            }
        }
        else if (Pattern.matches(regex5, str)) {
            try {
                objs[1] = sdf5.format(sdf5.parse(str.trim()));
                objs[0] = "1";
            }
            catch (Exception E) {
                objs[0] = "0";
                objs[1] = str+"不能转换为"+"dd-MM-yyyy格式的日期！";
            }
        }
        else if (Pattern.matches(regex6, str)) {
            try {
                objs[1] = sdf6.format(sdf6.parse(str.trim()));
                objs[0] = "1";
            }
            catch (Exception E) {
                objs[0] = "0";
                objs[1] = str+"不能转换为"+"dd_MM_yyyy格式的日期！";
            }
        }
        else if (Pattern.matches(regex7, str)) {
            try {
                objs[1] = sdf7.format(sdf7.parse(str.trim()));
                objs[0] = "1";
            }
            catch (Exception E) {
                objs[0] = "0";
                objs[1] = str+"不能转换为"+"dd/MM/yyyy格式的日期！";
            }
        }
        else {
            objs[0] = "0";
            objs[1] = str+"日期格式不正确或格式未定义！";
        }
        return objs;
    }
}
